<?php

namespace app\api\controller;

use think\Controller;
use think\exception\Exception;
use think\Request;
use app\api\model\MarkeModel;
use app\http\TokenTrait;

class Marke extends Common {

    use TokenTrait;

    /**
     * 积分规则
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function integral(MarkeModel $model) {
        $list = $model->getIntegral();
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 积分规则 -----新增
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function integralAdd(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['ctime'] = time();
        $list = $model->getIntegralAdd($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 积分规则 -------编辑
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function integralEdit($id, Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['utime'] = time();
        $list = $model->getIntegralEdit($id, $data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    //积分规则---查找商品
    public function integralShop(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['name'] = isset($data['name']) ? $data['name'] : '';
        $list = $model->getIntegralShop($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 积分规则 -------详情
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function integralInfo($id, MarkeModel $model) {
        $list = $model->getIntegralInfo($id);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 积分规则 -------删除
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function integralDel($id, Request $request, MarkeModel $model) {
        $list = $model->getIntegralDel($id);
        if ($list == 1) {
            return $this->returnJson('500', '该规则正在使用,无法删除');
        } else {
            return $this->returnJson('200', '成功');
        }
    }

    /**
     * 会员卡信息
     * @param type $id
     * @param Request $request
     */
    public function memberCardInfo() {
        $data = db('member_card')->order('id', 'desc')->find();
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 会员卡编辑
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function memberCard(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['ctime'] = time();
        $id = db('member_card')->order('id', 'desc')->field('id')->find();
        $list = $model->getMemberCard($id['id'], $data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 限时折扣  ----列表
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discount(Request $request, MarkeModel $model) {
        $data = $request->param();
        $list = $model->getDiscount($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 限时折扣  ----新增
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discountAdd(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $data['maker'] = self::getUId(); //制单人id
        $data['status'] = 1;
        $list = $model->getDiscountAdd($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 限时折扣  ----编辑
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discountEdit($id, Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $data['maker'] = self::getUId(); //制单人id
        $data['status'] = 1;
        $list = $model->getDiscountEdit($id, $data);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法编辑');
        }
    }

    /**
     * 限时折扣  ----查找
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discountInfo($id, MarkeModel $model) {
        $list = $model->getDiscountInfo($id);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    public function discountShop(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['name'] = isset($data['name']) ? $data['name'] : '';
        $list = $model->getDiscountshop($data);
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 选择商品
     * @param Request $request
     * @param MarkeModel $model
     */
    public function getCommodityList(Request $request, MarkeModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $name = isset($param['name']) ? $param['name'] : '';
        $status = isset($param['status']) ? $param['status'] : -1;
        $category = isset($param['category']) ? $param['category'] : 0;
        $brand = isset($param['brand']) ? $param['brand'] : '';
        $min_price = isset($param['min_price']) ? $param['min_price'] : '';
        $max_price = isset($param['max_price']) ? $param['max_price'] : '';
        //处理查询条件
        $where = '';
        if (!empty($name)) {
            $where .= 'name like "%' . $name . '%" and ';
        }
        if ($status != -1) {
            $where .= 'status = ' . $status . ' and ';
        }
        if (!empty($category)) {
            $where .= 'category = ' . $category . ' and ';
        }
        if (!empty($brand)) {
            $where .= 'brand = ' . $brand . ' and ';
        }
        if (!empty($min_price)) {
            $where .= 'retail_price >= ' . $min_price . ' and ';
        }
        if (!empty($max_price)) {
            $where .= 'retail_price <= ' . $max_price . ' and ';
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getCommodityList($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 获取商品下单品列表
     * @param type $request
     * @param MarkeModel $model
     */
    public function getProductList(Request $request, MarkeModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 1000;
        $com_id = isset($param['com_id']) ? $param['com_id'] : 0;
        if (empty($com_id)) {
            return $this->returnJson(500, 'com_id不能为空');
        }
        $data = $model->getProductList($com_id, $page, $size);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 限时折扣  ----删除
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discountDel($id, MarkeModel $model) {
        $list = $model->getDiscountDel($id);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法删除');
        }
    }

    /**
     * 限时折扣  ----撤销
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function discountRevoke($id, MarkeModel $model) {
        $list = $model->getDiscountRevoke($id);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法撤销');
        }
    }

    /**
     * 优惠券  ---- 列表
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function Coupon(Request $request, MarkeModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $status = isset($param['status']) ? $param['status'] : 1;
        $list = $model->getCouponList($page, $size, $status);

        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 优惠券  ---- 新增
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function CouponAdd(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : '';
        $data['fail_time'] = isset($data['fail_time']) ? strtotime($data['fail_time']) : '';
        $data['status'] = 1;
        $data['goods'] = json_encode($data['goods']);
        $list = $model->getCouponAdd($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 优惠券  ---- 修改
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function CouponEdit($id, Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : '';
        $data['fail_time'] = isset($data['fail_time']) ? strtotime($data['fail_time']) : '';
        $data['status'] = 1;
        $list = $model->getCouponEdit($id, $data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    public function CouponInfo($id, MarkeModel $model) {
        $list = $model->getCouponInfo($id);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 优惠券  ---- 删除
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function CouponDel($id, MarkeModel $model) {
        $list = $model->getCouponDel($id);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '优惠券无法删除');
        }
    }

    /**
     * 秒杀 ----- 列表
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckill(Request $request, MarkeModel $model) {
        $data = $request->param();
        $list = $model->getSeckill($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 秒杀 ----- 新增
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckillAdd(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $data['maker'] = self::getUId(); //制单人id
        $data['status'] = 1;
        $list = $model->getSeckillAdd($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 秒杀 ----- 修改
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckillEdit($id, Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $data['maker'] = self::getUId(); //制单人id
        $list = $model->getSeckillEdit($id, $data);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法编辑');
        }
    }

    public function seckillOnline($id, Request $request, MarkeModel $model) {
        try {
            $list = $model->getSeckillOnline($id);
            if (is_int($list)) {
                return $this->returnJson('200', 'success');
            } else {
                return $this->returnJson('500', 'fail');
            }
        } catch (\Exception $e) {
            return $this->returnJson('500', $e->getMessage());
        }
    }

    /**
     * 秒杀 ----- 查找
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckillInfo($id, MarkeModel $model) {
        $list = $model->getSeckillInfo($id);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 秒杀 ----- 停止
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckillStop($id, MarkeModel $model) {
        $list = $model->getSeckillStop($id);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法停止');
        }
    }

    /**
     * 秒杀 ----- 删除
     * @param Request $request
     * @param MarkeModel $model
     * @return \think\response\Json
     */
    public function seckillDel($id, MarkeModel $model) {
        $list = $model->getSeckillDel($id);
        if ($list == 1) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '活动无法删除');
        }
    }

    //编辑分销规则
    public function retailEdit(Request $request, MarkeModel $model) {
        $param = $request->param();
        $data = [];
        if (isset($param['status']) && !empty($param['status'])) {
            $data['status'] = $param['status'];
        }
        if (isset($param['first_retail'])) {
            $data['first_retail'] = $param['first_retail'];
        }
        if (isset($param['two_retail'])) {
            $data['two_retail'] = $param['two_retail'];
        }
        if (isset($param['goods'])) {
            $data['goods'] = $param['goods'];
        }
        if (isset($param['custom'])) {
            $data['custom'] = $param['custom'];
        }
        if (isset($param['zizhi'])) {
            $data['zizhi'] = $param['zizhi'];
        }
        $list = $model->getRetailEdit($data);
        if ($list !== FALSE) {
            return $this->returnJson('200', '成功');
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    //获取分销规则
    public function retailInfo(MarkeModel $model) {
        $list = $model->getRetailInfo();
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    public function retailGoods(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['name'] = isset($data['name']) ? $data['name'] : '';
        $list = $model->getretailGoods($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    public function retailCustomer(Request $request, MarkeModel $model) {
        $data = $request->param();
        $data['name'] = isset($data['name']) ? $data['name'] : '';
        $list = $model->getretailCustomer($data);
        if (!empty($list)) {
            return $this->returnJson('200', '成功', $list);
        } else {
            return $this->returnJson('500', '失败');
        }
    }

    /**
     * 分销列表
     * @param Request $request
     */
    public function retailList(Request $request, MarkeModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $start_time = isset($param['start_time']) ? strtotime($param['start_time']) : 0; //起始时间
        $end_time = isset($param['end_time']) ? strtotime($param['end_time']) : 0; //截止时间
        $order_num = isset($param['order_num']) ? $param['order_num'] : 0; //订单编号
        $user_cate_id = isset($param['user_cate_id']) ? $param['user_cate_id'] : 0; //客户分类id
        $status = isset($param['status']) ? $param['status'] : 0; //分销状态:0-全部，1-未完成，2-已取消，3-佣金未转账，4-佣金已转账
        $name_mobile = isset($param['name_mobile']) ? $param['name_mobile'] : 0; //客户名称或手机号码
        $earn_status = isset($param['earn_status']) ? $param['earn_status'] : 0; //转账状态：0-全部，1-卖家确认，2-客户确认，3-都未确认
        //处理查询条件
        $where = '';
        if (!empty($start_time)) {
            $where = 'l.ctime >= ' . $start_time . ' and ';
        }
        if (!empty($end_time)) {
            $where = 'l.ctime <= ' . $end_time . ' and ';
        }
        if (!empty($order_num)) {
            $where = 'l.order_num = ' . $order_num . ' and ';
        }
        if (!empty($user_cate_id)) {
            $where = 'u.category = ' . $user_cate_id . ' and ';
        }
        if (!empty($status)) {
            $where = 'l.status = ' . $status . ' and ';
        }
        if (!empty($name_mobile)) {
            if (is_numeric($name_mobile)) {
                $where = 'o.phone like "%' . $name_mobile . '%" and ';
            } else {
                $where = 'u.name like "%' . $name_mobile . '%" and ';
            }
        }
        if (!empty($earn_status)) {
            switch ($earn_status) {
                case 1://卖家确认
                    $where = '(l.admin_type1 = 1 or l.admin_type2 = 1) and ';
                    break;
                case 2://客户确认
                    $where = '(l.admin_type1 = 2 or l.admin_type2 = 2) and ';
                    break;
                case 3://都未确认
                    $where = 'l.retail_time1 = 0 and l.retail_time2 = 0 and ';
                    break;
                default:
                    break;
            }
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getRetailLogsList($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 分销情况统计
     */
    public function retailTotal(MarkeModel $model) {
        $data = $model->getRetailTotal();
        return $this->returnJson(200, '成功', $data);
    }

    //测试用
    public function test() {
        $data = [
            'name' => uniqid(),
            'item' => 1,
            'goods_code' => uniqid(),
            'status' => 0,
            'color' => 13,
            'size' => 19,
            'unit' => 19,
            'material' => 1,
            'season' => 1,
            'style' => 1,
            'brand' => 1,
            'category' => 1,
            'sex' => 1,
            'price' => 10,
            'year' => 2018,
            'sort' => 10,
        ];
//        $res = db('shop')->insertGetId($data);
//        var_dump($res);
    }

}
